package com.sinosoft.perfect.system._authorization.dao;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseSqlSessionDaoSupport;
import com.sinosoft.perfect.system._.po.TUserRolesysRelation;
import com.sinosoft.perfect.system._authorization.MapperSystemAuthorization;

public class UserRolesysAuthorizeDaoImpl extends BaseSqlSessionDaoSupport implements IUserRolesysAuthorizeDao {

	private MapperSystemAuthorization getMapperSystemAuthorization(){
		return getSqlSession().getMapper(MapperSystemAuthorization.class);
	}
	
	@Override
	public int deleteRolesystemRelationsByUser (String userId) throws SQLException{
		return getMapperSystemAuthorization().deleteRolesystemRelationsByUser(userId);
	}

	@Override
	public int deleteRolesystemRelationsByUsersOfUnit(String unitId)
			throws SQLException {
		return getMapperSystemAuthorization().deleteRolesystemRelationsByUsersOfUnit(unitId);
	}

	@Override
	public void insertByTUserRolesysRelation(
			TUserRolesysRelation tuserRolesysRelation) throws SQLException {
		getMapperSystemAuthorization().insertByTUserRolesysRelation(tuserRolesysRelation);
	}

	@Override
	public List<TUserRolesysRelation> queryRolesystemsOfUser(String userId)
			throws SQLException {
		return getMapperSystemAuthorization().queryRolesystemsOfUser(userId);
	}

	@Override
	public List<String> queryUserIdsOfUnit(String unitId) throws SQLException {
		return getMapperSystemAuthorization().queryUserIdsOfUnit(unitId);
	}

	@Override
	public void delAllRoleUsersOfUnitAndAddUsersRoleOfUnit(String unitId, List<TUserRolesysRelation> tuserRolesysRelations)
			throws SQLException {
		getMapperSystemAuthorization().deleteRolesystemRelationsByUsersOfUnit(unitId);
		if(tuserRolesysRelations != null){
			for(int i=0;i<tuserRolesysRelations.size();i++){
				TUserRolesysRelation tuserRolesysRelation = tuserRolesysRelations.get(i);
				if(tuserRolesysRelation.getRoleSystemId() != null && !"".equals(tuserRolesysRelation.getRoleSystemId()+""))
					getMapperSystemAuthorization().insertByTUserRolesysRelation(tuserRolesysRelation);
			}
		}
	}

	@Override
	public void delAllRolesOfUserAndAddUserRoles(String userId, List<TUserRolesysRelation> tuserRolesysRelations)
			throws SQLException {
		getMapperSystemAuthorization().deleteRolesystemRelationsByUser(userId);
		if(tuserRolesysRelations != null){
			for(int i=0;i<tuserRolesysRelations.size();i++){
				TUserRolesysRelation tuserRolesysRelation = tuserRolesysRelations.get(i);
				if(tuserRolesysRelation.getRoleSystemId() != null && !"".equals(tuserRolesysRelation.getRoleSystemId()+""))
					getMapperSystemAuthorization().insertByTUserRolesysRelation(tuserRolesysRelation);
			}
		}
	}

	@Override
	public void deleteTUserRolesysRelation(TUserRolesysRelation tUserRolesysRelation) throws SQLException {
		getMapperSystemAuthorization().deleteTUserRolesysRelation(tUserRolesysRelation);
	}

}
